package com.unstoppable.submitbutton.slice;

import com.unstoppable.submitbuttonview.SubmitButton;
import com.unstoppable.submitbuttonview.utils.LogUtil;
import com.unstoppable.submitbutton.ResourceTable;
import com.unstoppable.submitbutton.base.BaseAbilitySlice;
import com.unstoppable.submitbutton.utils.Toast;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Switch;

public class MainAbilitySlice extends BaseAbilitySlice implements Component.ClickedListener {
    private SubmitButton sBtnLoading, sBtnProgress;
    private Button btnSucceed, btnFailed, btnReset;
    private Switch mSwitch;

    @Override
    protected int getLayout() {
        return ResourceTable.Layout_submit_button;
    }

    @Override
    protected void initView() {
        sBtnLoading = (SubmitButton) findComponentById(ResourceTable.Id_sbtn_loading);
        sBtnProgress = (SubmitButton) findComponentById(ResourceTable.Id_sbtn_progress);
        mSwitch = (Switch) findComponentById(ResourceTable.Id_switch1);

        btnFailed = (Button) findComponentById(ResourceTable.Id_btn_failed);
        btnSucceed = (Button) findComponentById(ResourceTable.Id_btn_succeed);
        btnReset = (Button) findComponentById(ResourceTable.Id_btn_reset);
        sBtnLoading.setClickedListener(this);
        sBtnProgress.setClickedListener(this);
        btnFailed.setClickedListener(this);
        btnSucceed.setClickedListener(this);
        btnReset.setClickedListener(this);

        mSwitch.setCheckedStateChangedListener((absButton, b) -> {
            if (b) {
                sBtnLoading.setVisibility(Component.HIDE);
                sBtnProgress.setVisibility(Component.VISIBLE);
                sBtnLoading.reset();
            } else {
                sBtnLoading.setVisibility(Component.VISIBLE);
                sBtnProgress.setVisibility(Component.HIDE);
                sBtnProgress.reset();
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(Component v) {
        switch (v.getId()) {
            case ResourceTable.Id_sbtn_loading:
                Toast.show(getContext(), "SubmitButton be clicked");
                break;
            case ResourceTable.Id_sbtn_progress:
                LogUtil.debug("倒计时---点击","触发");
                getContext().getUITaskDispatcher().asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        sBtnProgress.doResult(true);
                    }
                });
                break;
            case ResourceTable.Id_btn_succeed:
                LogUtil.error("SubmitButton", "成功");
                if (mSwitch.isChecked()) {
                    sBtnProgress.loadingDoResult(true);
                } else {
                    sBtnLoading.doResult(true);
                }
                break;
            case ResourceTable.Id_btn_failed:
                if (mSwitch.isChecked()) {
                    sBtnProgress.loadingDoResult(false);
                } else {
                    sBtnLoading.doResult(false);
                }
                break;
            case ResourceTable.Id_btn_reset:
                if (mSwitch.isChecked()) {
                    sBtnProgress.reset();
                } else {
                    sBtnLoading.reset();
                }

                break;
        }
    }
}
